#!/bin/bash

binary=lib_cl_demo
dyn_libfile=liblwbt.so
stat_libfile=liblwbt.a

# TODO: use these flags rather than the setup hard-coded in the gcc lines
INC_FLAGS="-I ../include/"
DEF_FLAGS="-DMEM_LIBC_MALLOC=1 -DLWIP_NOASSERT=1 -DLWIP_ERROR=1"

echo " "
echo "building library ..."
echo " "

cd src/

gcc ${INC_FLAGS} -fno-common -bundle -dynamiclib -dynamic -fPIC -g -c -Wall libAPI.c -DMEM_LIBC_MALLOC=1 -DLWIP_NOASSERT=1 -DLWIP_ERROR=1 
gcc ${INC_FLAGS} -fno-weak -fno-common -dynamic -shared -dynmaiclib -Wall -o ${dyn_libfile} libAPI.o secret.lib -lc -DMEM_LIBC_MALLOC=1 -DLWIP_NOASSERT=1 -DLWIP_ERROR=1 

# build static lib from object file
ar -rv ${stat_libfile} libAPI.o

echo " "
echo "building standalone binary ..."
echo " "

# build against dynmic library
#gcc -o ${binary} -I include/ main.c -DMEM_LIBC_MALLOC=1 -DLWIP_NOASSERT=1 -DLWIP_ERROR=1 -DDEBUG_HIGH ${dyn_libfile}  -g -mcpu=arm1176jzf-s

# build against static library
gcc -o ${binary} ${INC_FLAGS} main.c -DMEM_LIBC_MALLOC=1 -DLWIP_NOASSERT=1 -DLWIP_ERROR=1 -DDEBUG_HIGH ${stat_libfile}  secret.lib -g -mcpu=arm1176jzf-s


# Code sign both the stand-alone exectuable as well as dynamic library and relocate
ldid -S ${binary} 
ldid -S ${dyn_libfile} 

mkdir -p ../lib/
mv ${dyn_libfile} ../lib/.
mv ${stat_libfile} ../lib/.

mkdir -p ../bin/
mv ${binary} ../bin/.

# Clear out intermediate object files
rm *.o



